package io.renren.common.utils;

import cn.hutool.core.lang.tree.Tree;

import java.util.*;

/**
 * 表单格式化工具
 * */
public class TableFormat {
    /**
     * 行转列
     * */
    public static Collection<Map > rowToCol(List<Map> mapList, String groupNames , String colName
            , String valueName,Set<String> colSet){
        Map<Object,Map> table = new TreeMap<>();
        String[] groupNamesArray = groupNames.split(",");
        for( Map obj : mapList ){
            Object name = obj.get(groupNamesArray[0]);
            Object col = obj.get(colName);
            Map line = table.get(name);
            if(line==null){
                line = new HashMap();
                table.put(name,line);
                line.putAll(obj);
                //line.put(idName,name);
            }
            colSet.add(col.toString());
            line.put(col,obj.get(valueName));
        }
        return table.values();
    }

    public static void main(String[] args) {
        Map<String,Object> c = new HashMap();
        c.put("name","小明");
        c.put("class","语文");
        c.put("score","1");
        Map<String,Object> c2 = new HashMap();
        c2.put("name","小明");
        c2.put("class","数学");
        c2.put("score","1");
        Map<String,Object> c3 = new HashMap();
        c3.put("name","小红3");
        c3.put("class","语文");
        c3.put("score","11");
        List<Map> list = new ArrayList<>();
        list.add(c);
        list.add(c2);
        list.add(c3);
        System.out.println(rowToCol(list,"name","class","score",new TreeSet<String>()));
    }

}
